package com.yidu.t299.dao.fxj;


import com.yidu.t299.domain.fxj.LessonDomain;
import com.yidu.t299.domain.fxj.Speciality;
import com.yidu.t299.domain.fxj.TextbookStock;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface LessoncMapper {
    @Select("select * from lesson where 1=1 ${condition} limit #{page},#{rows}")
    @Results(id = "kk",value = {
            @Result(id = true,column = "lesson_id",property = "lessonId"),
            @Result(column = "lesson_name",property = "lessonName"),
            @Result(column = "period_times",property = "periodTimes"),
            @Result(column = "lesson_times",property = "lessonTimes"),
            @Result(column = "speciality_id",property = "speciality",
                    one = @One(select = "com.yidu.t299.dao.fxj.LessoncMapper.getSpecialityId")),
            @Result(column = "stock_id",property = "textbookStock",
                    one = @One(select = "com.yidu.t299.dao.fxj.LessoncMapper.getTextbookStockId"))
    })
    public List<LessonDomain> selectByPrimaryKey(int rows, int page, String condition);


    //拿到speciality(根据科目id查找科目信息)
    @Select("select * from speciality where speciality_id = #{specialityId,jdbcType=INTEGER}")
    @Results(id = "specialityObj",value = {
            @Result(id = true,column = "speciality_id",property = "specialityId"),
            @Result(column = "speciality_name",property = "specialityName")
    })
    public Speciality getSpecialityId(int specialityId);

    //根据id查书信息
    @Select("select * from textbook_stock where stock_id = #{stockId,jdbcType=INTEGER}")
    @Results(id = "textbookStockObj",value = {
            @Result(id = true,column = "stock_id",property = "stockId"),
            @Result(column = "textbook_name",property = "textbookName")
    })
    public TextbookStock getTextbookStockId(int stockId);


    /**
     * 删除
     * @return 影响的行数(返回1则为成功)
     */
    @Delete({
            "delete from lesson" +
                    " where lesson_id = #{lessonId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer lessonId);

    /**
     * 添加
     * @return 影响的行数(返回1则为成功)
     */
    @Insert({
            "insert into lesson (lesson_id,lesson_name,period_times,lesson_times,speciality_id,stock_id) " +
                    " values (#{lessonId,jdbcType=INTEGER},#{lessonName,jdbcType=VARCHAR},#{periodTimes,jdbcType=INTEGER}," +
                    "#{lessonTimes,jdbcType=INTEGER},#{specialityId,jdbcType=INTEGER},#{stockId,jdbcType=INTEGER})"
    })
    int insert(LessonDomain lessonDomain);

    /**
     * 查询
     * @return 对象
     */
    @Select({
            "select " +
                    " lesson_id,lesson_name,period_times,lesson_times,speciality_id,stock_id " +
                    " from lesson " +
                    " where lesson_id = #{lessonId,jdbcType=INTEGER}"
    })
    @Results(id = "LessoncMap",value = {
            @Result(id = true,column = "lesson_id",property = "lessonId"),
            @Result(column = "lesson_name",property = "lessonName"),
            @Result(column = "period_times",property = "periodTimes"),
            @Result(column = "lesson_times",property = "lessonTimes"),
            @Result(column = "speciality_id",property = "specialityId"),
            @Result(column = "stock_id",property = "stockId")
    })
    LessonDomain selectByKey(Integer lessonId);


    /**
     * 行数
     */
    @Select("select count(*) from (select lesson_id,lesson_name,period_times,lesson_times,speciality_name ,textbook_name from lesson " +
            "join speciality on speciality.speciality_id = lesson.speciality_id " +
            "join textbook_stock on textbook_stock.stock_id = lesson.stock_id) p where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 修改
     * @return 影响的行数(返回1则为成功)
     */
    @Update({
            "update lesson",
            " set lesson_name = #{lessonName,jdbcType=VARCHAR},period_times = #{periodTimes,jdbcType=INTEGER}, " +
            " lesson_times = #{lessonTimes,jdbcType=INTEGER},speciality_id = #{specialityId,jdbcType=INTEGER}, stock_id = #{stockId,jdbcType=INTEGER} ",
            " where lesson_id = #{lessonId,jdbcType=INTEGER} "
    })
    int updateByPrimaryKey(LessonDomain lessonDomain);

}
